package com.atguigu.day01;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Example5 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .flatMap(new FlatMapFunction<ClickEvent, ClickEvent>() {
                    @Override
                    public void flatMap(ClickEvent value, Collector<ClickEvent> out) throws Exception {
                        if (value.username.equals("Mary")) {
                            out.collect(value);
                            out.collect(value);
                        } else if (value.username.equals("Bob")) {
                            out.collect(value);
                        }
                    }
                })
                .print();

        env
                .addSource(new ClickSource())
                .flatMap((ClickEvent value, Collector<ClickEvent> out) -> {
                    if (value.username.equals("Mary")) {
                        out.collect(value);
                        out.collect(value);
                    } else if (value.username.equals("Bob")) {
                        out.collect(value);
                    }
                })
                // Collector<Object> out
                .returns(Types.POJO(ClickEvent.class))
                .print();

        env.execute();
    }
}
